//提交校验
function saleValidParam(){
	var saleSecurNum = document.getElementById("saleSecurNum").value;
	if(!saleSecurNum){
		alert("请输入证券代码。");
		return;
	}
	var salePrice = document.getElementById("salePrice").value;
	if(!salePrice){
		alert("请输入卖出价格。");
		return;
	}
	var serialNumber = document.getElementById("serialNumber").value;
	if(!serialNumber){
		alert("请输入订单号。");
		return;
	}
	var saleCount = document.getElementById("saleCount").value;
	if(!saleCount){
		alert("请输入卖出数量。");
		return;
	}
	var saleAppointmentNumber = document.getElementById("saleAppointmentNumber").value;
	if(!saleAppointmentNumber){
		alert("请输入约定号。");
		return;
	}
	var saleSeatOfTheOtherParty = document.getElementById("saleSeatOfTheOtherParty").value;
	if(!saleSeatOfTheOtherParty){
		alert("请输入对方席位。");
		return;
	}
	var saleMutualShareholder = document.getElementById("saleMutualShareholder").value;
	if(!saleMutualShareholder){
		alert("请输入对方股东。");
		return;
	}
	var saleSecurNumError = document.getElementById("saleSecurNumError").innerHTML;
	if(!!saleSecurNumError){
		alert("证券代码不合法，请确认。");
		return;
	}
	var salePriceError = document.getElementById("salePriceError").value;
	if(!!salePriceError){
		alert("卖出价格不合法，请确认。");
		return;
	}
	var saleCountError = document.getElementById("saleCountError").value;
	if(!!saleCountError){
		alert("卖出数量不合法，请确认。");
		return;
	}
	saleSubmit();
}
// 卖出提交
function saleSubmit() {
	var options = {
		"accountNumber" : localStorage.accountNum,
		"token" : localStorage.token,
		"stockCode" : document.getElementById("saleSecurNum").value,
		"buyingAndSellingPrice" : document.getElementById("salePrice").value,
		"quantity" : document.getElementById("saleCount").value,
		"seclarationType" : "20002",
		"appointmentNumber": document.getElementById("saleAppointmentNumber").value,
		"seatOfTheOtherParty":document.getElementById("saleSeatOfTheOtherParty").value,
		"mutualShareholder":document.getElementById("saleMutualShareholder").value,
		"serialNumber":document.getElementById("serialNumber").value,
		
	};
	var para = JSON.stringify(options);
	$.ajax({
		type : "POST",
		url : "/ThreeNewBoard/buyingstock",
		data : para,
		crossDomain : true,
		async : true,
		contentType : "application/json",
		dataType : "json",
		success : function(result) {
			var data = result.data
			if (data.status) {
				// 查询到的数据
				alert(data.promptMessage);
				queryHistory();
				queryUserInfo("child");
			} else {
				alert(data.promptMessage);
			}
		}
	});
}

var historyOrder = [];
var currentPage = 1;
var totalPage = 1;
// 查询卖出交易记录
function queryHistory(type) {
	var options = {
		"stockStatus" : "50002",
		"accountNumber" : localStorage.accountNum,
		"token" : localStorage.token
	};
	$.ajax({
		type : "POST",
		url : "/ThreeNewBoard/queryorders",
		data : JSON.stringify(options),
		crossDomain : true,
		async : true,
		contentType : "application/json",
		dataType : "json",
		success : function(result) {
			if (result.message == "SUCCESS") {
				// data为请求到的数据
				var data = result.data;
				// 先打个桩
				historyOrder = data;
				totalPage = Math.ceil(data.length / 10);
				if (totalPage === 0) {
					var noData = "<div style='width:100%;height:30px;margin:40px 0;text-align: center;'>暂无表格数据</div>"
					document.getElementById("saleNoData").innerHTML = noData;
				}
				document.getElementById("saleTotal").innerText="";
				$("#saleTotal").append(totalPage);
				dealData();
			} else {
				alert("数据请求错误！");
			}
		}
	});
}

// 处理交易记录表格数据展示
function dealData() {
	var displayOrder = [];
	$("#saleDisplayData").find("tr").remove();
	if (totalPage < 1 || currentPage === totalPage) {
		$("#saleNextPageBtn").attr("disabled", true);
	} else {
		$("#saleNextPageBtn").attr("disabled", false);
	}
	if (currentPage === 1) {
		$("#salePrePageBtn").attr("disabled", true);
	} else {
		$("#salePrePageBtn").attr("disabled", false);
	}
	document.getElementById("saleCurrentPage").innerText = currentPage;
	for (var i = (currentPage - 1) * 10; i < currentPage * 10; i++) {
		var item = historyOrder[i];
		if (!item) {
			return;
		}
		displayOrder.push(item);
		row = document.createElement("tr"); // 创建行 
		td1 = document.createElement("td"); // 单号
		td2 = document.createElement("td"); // 数量
		td3 = document.createElement("td"); // 单价
		td4 = document.createElement("td"); // 账户
		td5 = document.createElement("td"); // 价格
		td6 = document.createElement("td"); // 状态
		td7 = document.createElement("td"); // 证券号
		td8 = document.createElement("td"); // 证券名称
		td9 = document.createElement("td"); // 关联持仓流水号
		td1.appendChild(document.createTextNode(item.sellingSerialNumber)); // 为单元格添加内容 
		td2.appendChild(document.createTextNode(item.quantity));
		td3.appendChild(document.createTextNode(item.sellTime));
		td4.appendChild(document.createTextNode(item.accountNumber));
		td5.appendChild(document.createTextNode(item.sellUnitPrice));
		td6.appendChild(document.createTextNode(item.stockStatus));
		td7.appendChild(document.createTextNode(item.stockCode));
		td8.appendChild(document.createTextNode(item.stockCodeName));
		td9.appendChild(document.createTextNode(item.serialNumber));
		row.appendChild(td1);
		row.appendChild(td2);
		row.appendChild(td3);
		row.appendChild(td4);
		row.appendChild(td5);
		row.appendChild(td6);
		row.appendChild(td7);
		row.appendChild(td8);
		row.appendChild(td9);
		$("#saleDisplayData").append(row);

	}
}
// 上一页
function prePage() {
	currentPage--;
	dealData();
}
// 下一页
function nextPage() {
	currentPage++;
	dealData();
}